package com.qiangesoft.security.service;

import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

import java.util.Arrays;

/**
 * 自定义用户信息服务类
 *
 * @author qiangesoft
 * @date 2024-05-14
 */
public class CustomUserDetailsService implements UserDetailsService {

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        // todo 数据库查询逻辑
        CustomUserDetails userDetails = new CustomUserDetails();
        if ("admin".equals(username)) {
            userDetails.setId(1L);
            userDetails.setUsername("admin");
            userDetails.setPassword(new BCryptPasswordEncoder().encode("123456"));
            userDetails.setRoles(Arrays.asList("admin"));
            userDetails.setPermissions(Arrays.asList("add", "delete", "update"));
        } else {
            userDetails.setId(2L);
            userDetails.setUsername("test");
            userDetails.setPassword(new BCryptPasswordEncoder().encode("123456"));
            userDetails.setRoles(Arrays.asList("common"));
            userDetails.setPermissions(Arrays.asList("select"));
        }
        return userDetails;
    }
}
